package org.hfy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.hfy.common.ApiResponse;
import org.hfy.dto.CustomerQueryDTO;
import org.hfy.entity.Customer;
import org.hfy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户管理Controller
 * 
 * @author hfy
 * @version 1.0
 */
@RestController
@RequestMapping("/customer")
@Validated
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 分页查询客户列表
     * 
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public ApiResponse<IPage<Customer>> getCustomerPage(CustomerQueryDTO queryDTO) {
        try {
            IPage<Customer> page = customerService.getCustomerPage(queryDTO);
            return ApiResponse.success(page);
        } catch (Exception e) {
            return ApiResponse.error("查询客户列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取客户详情
     * 
     * @param id 客户ID
     * @return 客户信息
     */
    @GetMapping("/{id}")
    public ApiResponse<Customer> getCustomerById(@PathVariable Long id) {
        try {
            Customer customer = customerService.getCustomerById(id);
            if (customer == null) {
                return ApiResponse.error(404, "客户不存在");
            }
            return ApiResponse.success(customer);
        } catch (Exception e) {
            return ApiResponse.error("获取客户详情失败：" + e.getMessage());
        }
    }

    /**
     * 新增客户
     * 
     * @param customer 客户信息
     * @return 操作结果
     */
    @PostMapping
    public ApiResponse<String> addCustomer(@Valid @RequestBody Customer customer) {
        try {
            boolean success = customerService.addCustomer(customer);
            if (success) {
                return ApiResponse.success("新增客户成功");
            } else {
                return ApiResponse.error("新增客户失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("新增客户失败：" + e.getMessage());
        }
    }

    /**
     * 更新客户
     * 
     * @param id 客户ID
     * @param customer 客户信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public ApiResponse<String> updateCustomer(@PathVariable Long id, @Valid @RequestBody Customer customer) {
        try {
            customer.setId(id);
            boolean success = customerService.updateCustomer(customer);
            if (success) {
                return ApiResponse.success("更新客户成功");
            } else {
                return ApiResponse.error("更新客户失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("更新客户失败：" + e.getMessage());
        }
    }

    /**
     * 删除客户
     * 
     * @param id 客户ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteCustomer(@PathVariable Long id) {
        try {
            boolean success = customerService.deleteCustomer(id);
            if (success) {
                return ApiResponse.success("删除客户成功");
            } else {
                return ApiResponse.error("删除客户失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("删除客户失败：" + e.getMessage());
        }
    }

    /**
     * 获取状态选项
     * 
     * @return 状态列表
     */
    @GetMapping("/statuses")
    public ApiResponse<List<Map<String, String>>> getCustomerStatuses() {
        try {
            List<Map<String, String>> statuses = Arrays.asList(
                new HashMap<String, String>() {{
                    put("value", "active");
                    put("label", "启用");
                }},
                new HashMap<String, String>() {{
                    put("value", "inactive");
                    put("label", "停用");
                }}
            );
            return ApiResponse.success(statuses);
        } catch (Exception e) {
            return ApiResponse.error("获取状态列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取业务经理列表
     * 
     * @return 业务经理列表
     */
    @GetMapping("/managers")
    public ApiResponse<List<Map<String, Object>>> getManagerList() {
        try {
            List<Map<String, Object>> managers = customerService.getManagerList();
            return ApiResponse.success(managers);
        } catch (Exception e) {
            return ApiResponse.error("获取业务经理列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有客户列表（不分页）
     * 
     * @return 所有客户列表
     */
    @GetMapping("/all")
    public ApiResponse<List<Customer>> getAllCustomers() {
        try {
            List<Customer> customers = customerService.getAllCustomers();
            return ApiResponse.success(customers);
        } catch (Exception e) {
            return ApiResponse.error("获取客户列表失败：" + e.getMessage());
        }
    }
} 